const app = getApp()
const audio = wx.createInnerAudioContext()
Page({
  data: {
    src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
    percent: 0,
    bgTime: '00:00',
    playTime: '00:00',
    isPlay: false,
    pic: '../../img/play1.png',
    info: ''
  },

  onLoad: function (options) {
    var page = this
    var cid = options.id
    console.log(cid);
    app.util.request({
      url: 'entry/wxapp/clockdetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: {
        id: cid
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res.data.data)
        if (res.data.code == 200) {
          var info = res.data.data
          console.log(info.sound)
          console.log(app.data.imgpath + info.sound)
          page.setData({
            info: info
          })
          audio.src = app.data.imgpath + info.sound
          audio.onPlay(() => {
            console.log('开始播放')
          })
          audio.onError(res => {
            console.log(res.errMsg)
            console.log(res.errCode)
          })
          audio.onTimeUpdate(() => {
            console.log('当前时间', audio.currentTime)
            console.log('时长:', audio.duration)
            const bgTime = page.secondToDate(audio.duration)
            const playTime = page.secondToDate(audio.currentTime)
            const percent = audio.currentTime / audio.duration
            page.setData({
              percent: percent * 100,
              bgTime: bgTime,
              playTime: playTime
            })
          })
        } else {
          wx.showToast({
            title: '网络错误',
            icon: 'success',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.hideLoading()
      }
    })



  },

  //保存图片到本地
  save() {
    wx.showLoading({
      title: 'Loading',
      mask: true
    })
    var page = this
    var url = page.data.info.cav_img
    console.log(app.data.imgpath + url);
    wx.getImageInfo({
      src: app.data.imgpath + url, //请求的网络图片路径
      success: function (res) {
        var savepath = res.path
        wx.saveImageToPhotosAlbum({
          //保存生成的图片到手机相册里
          filePath: savepath,
          success(res) {
            wx.hideLoading()
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          },
          fail: function (err) {
            console.log(err)
            wx.hideLoading()
            wx.showToast({
              title: '保存失败',
              icon: 'success',
              duration: 2000
            })

            if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
              console.log('当初用户拒绝，再次发起授权')
              wx.authorize({
                scope: 'scope.writePhotosAlbum'
              })
            }
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    })

  },
  unLoad() {
    audio.destroyed()
  },

  secondToDate(result) {
    var m = this.setDb(Math.floor((result / 60) % 60))
    var s = this.setDb(Math.floor(result % 60))

    return (result = m + ':' + s)
  },

  setDb(num) {
    //补零操作
    if (num < 10) {
      return '0' + num
    } else {
      return '' + num
    }
  },

  audioPlay() {
    if (!this.data.isPlay) {
      audio.play()
      this.setData({
        isPlay: true,
        pic: '../../img/play2.png'
      })
    } else {
      audio.pause()
      this.setData({
        isPlay: false,
        pic: '../../img/play1.png'
      })
    }
  }
})
